package com.itheima.springbootwebtlias.controller;

import com.itheima.springbootwebtlias.pojo.*;
import com.itheima.springbootwebtlias.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    //添加班级
    @PostMapping()
    public Result add(@RequestBody Clazz clazz){
        clazzService.addClazzs(clazz);
        return Result.success();
    }

    //查询所有班级
    @GetMapping("/list")
    public Result QueryAll(){
        List<Clazz> clazzes = clazzService.selectAll();
        return Result.success(clazzes);
    }

    //班级列表查询
    @GetMapping
    public Result page(ClazzQueryParam queryParam){
        //1.接收请求参数：kv格式
        //定义分页条件实体对象+kv格式参数
        log.info("分页条件查询，请求参数：{}",queryParam);
        //2.调用业务层功能方法，查询员工列表
        PageResult pageResult = clazzService.page(queryParam);
        //3.封装返回结果
        return Result.success(pageResult);
    }

    //删除班级
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        clazzService.deleteById(id);
        return Result.success();
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Clazz clazz = clazzService.findById(id);
        return Result.success(clazz);
    }

    //修改班级
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        clazzService.update(clazz);
        return Result.success();
    }



}
